import { createApp } from 'vue';
import App from './App.vue';
import { createPinia } from 'pinia'
// 引入路由
import Router from '@/routers/index';
const pinia = createPinia()
// 引入Vant UI
// 引入全部组件
import Vant from 'vant';
// 引入组件样式
import 'vant/lib/index.css';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

import Stores from '@/stores/Stores';

import Rules from '@/routers/rules';

import Tools from '@/tools/tools';

import request from '@/services/request';

// import { CountDown } from 'vant';
const app = createApp(App)
.use(Router)
.use(Vant)
.use(pinia.use(piniaPluginPersistedstate))
.use(Stores)
.use(Tools)
.use(request)
// .use(CountDown)
.mount('#app');

Router.beforeEach(async (to,from) => {
    let path = to.path ?? '';

    if(Rules.includes(path))
    {
        return true;
    }

    let business = app.$business.value ? app.$business.value : {};

    if(Object.getOwnPropertyNames(business).length === 0)
        {
            // 提示信息
            app.$warning('请先登录');
    
            // 重定向登录页面
            return {path:'/business/login'};
        }

    let id = business.id ?? 0;

    let mobile = business.mobile ?? 0;

    let result = await app.$http.post({
        url:'/business/check',
        params:{
            id,
            // mobile
        }
    })

    if(result.code === 1)
        {
            app.$business.value = result.data;
    
            return true;
        }else{
    
            app.$warning(result.msg);
    
            app.$ResetBusiness();
    
            return {path:'/business/login'};
        }

})